package com.sojson.util.mail.interfaces.impl;

import java.security.GeneralSecurityException;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;

import com.sojson.constant.Constant;
import com.sojson.util.SpringUtil;
import com.sojson.util.StringUtil;
import com.sojson.util.mail.MailTemplate;
import com.sojson.util.mail.interfaces.MailUtilInterface;
import com.sun.mail.util.MailSSLSocketFactory;

/**
 * 发送邮件工具类实现类
 * 
 * @author liu
 * @Date 2020-06-28 15:26:37
 * @Description
 *
 */
public class MailUtilImpl implements MailUtilInterface {

    private static MailUtilImpl mailUtilsImpl;
    /** 发送邮件的主机 */
    private final String host = Constant.PROPERTIES.getProperty("so.mail.host");
    /** 发件人邮箱号码 */
    private final String mail = Constant.PROPERTIES.getProperty("so.mail.mail");
    /** 发件人邮箱帐号 */
    private final String username = Constant.PROPERTIES.getProperty("so.mail.username");
    /** 发件人邮箱的授权码 */
    private final String code = Constant.PROPERTIES.getProperty("so.mail.code");
    private final MailSends sends = SpringUtil.getBean("mailSends", MailSends.class);

    private MailUtilImpl() {}

    /**
     * 发送一封邮件
     * 
     * @param target 目标邮箱
     * @param title 邮件标题，从MailTemplate类获取
     * @param content 内容，从MailTemplate类获取
     * @param param 参数
     * @throws GeneralSecurityException
     */
    @Override
    public void send(String target, MailTemplate template, Object... param) throws GeneralSecurityException {
        Session session = createSession(getHost(), getUsername(), getFromCode());
        sends.run(getFrom(), target, template.getTitle(),
            param.length == 0 ? template.getContent() : String.format(template.getContent(), param), null, session);
    }

    /**
     * 发送多封邮件
     * 
     * @param targets 目标邮箱
     * @param title 邮件标题，从MailTemplate类获取
     * @param content 内容，从MailTemplate类获取
     * @param param 参数
     * @throws GeneralSecurityException
     */
    @Override
    public void send(String[] targets, MailTemplate template, Object... param) throws GeneralSecurityException {
        Session session = createSession(getHost(), getUsername(), getFromCode());
        for (int i = 0; i < targets.length; i++) {
            sends.run(getFrom(), targets[i], template.getTitle(),
                param.length == 0 ? template.getContent() : String.format(template.getContent(), param), i, session);
        }
    }

    /**
     * 创建session对象
     * 
     * @param host
     * @param username
     * @param fromCode
     * @return
     * @throws GeneralSecurityException
     */
    private Session createSession(String host, String username, String fromCode) throws GeneralSecurityException {
        // String host = "smtp.qq.com"; // 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易)
        Properties properties = System.getProperties();// 获取系统属性

        properties.setProperty("mail.smtp.host", host);// 设置邮件服务器
        properties.setProperty("mail.smtp.auth", "true");// 打开认证

        if ("smtp.qq.com".equals(host)) {
            // QQ邮箱需要下面这段代码，163邮箱不需要
            MailSSLSocketFactory sf = null;
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);
        }

        // 1.获取默认session对象
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, fromCode);// 发件人邮箱账号、授权码
            }
        });
        // Session session = Session.getDefaultInstance(properties, new Authenticator() {
        // @Override
        // public PasswordAuthentication getPasswordAuthentication() {
        // return new PasswordAuthentication(username, fromCode); // 发件人邮箱账号、授权码
        // }
        // });
        return session;
    }

    /**
     * 获取邮件服务器
     * 
     * @return
     */
    private String getHost() {
        return host;
    }

    /**
     * 获取发件人邮箱账号
     * 
     * @return
     */
    private String getUsername() {
        return username;
    }

    /**
     * 获取发件人邮箱授权码
     * 
     * @return
     */
    private String getFromCode() {
        return code;
    }

    /**
     * 获取发件人电子邮箱
     * 
     * @return
     */
    private String getFrom() {
        return mail;
    }

    /**
     * 设置邮件服务器
     * 
     * @return
     */
    public void setHost() {}

    /**
     * 设置发件人邮箱账号
     * 
     * @return
     */
    public void setUsername() {}

    /**
     * 设置发件人邮箱授权码
     * 
     * @return
     */
    public void setFromCode() {}

    /**
     * 设置发件人电子邮箱
     * 
     * @return
     */
    public void setFrom() {}

    /**
     * 判断一个对象是否是空(空对象)
     * 
     * @param obj
     * @return
     */
    private static boolean isBlankObject(Object obj) {
        return StringUtil.isBlankObject(obj);
    }

    public static MailUtilImpl getInstances() {
        if (isBlankObject(mailUtilsImpl)) {
            mailUtilsImpl = new MailUtilImpl();
        }
        return mailUtilsImpl;
    }

}